/*
 * Conditioner.h
 *
 *  Created on: Dec 11, 2011
 *      Author: Michael Anderson
 *      Part of Team 955 Battery Cart project
 *      compiled on Eclipse with Arduino libraries and avr-gcc
 */

#ifndef CONDITIONER_H_
#define CONDITIONER_H_

#include <Arduino.h>
#include <HardwareSerial.h>
#include "Battery.h"
#include "EventManager.h"
#include "Utils.h"
#include "pins.h"
#include "enums.h"
#include "Timer.h"

static const analog_read_filter_type ANALOG_FILTER_TYPE = ANALOG_READ_FILTER_RMS;

class Conditioner {
public:
    Conditioner(uint8_t, EventManager&);

    void run();

    bool connect(Battery&);
    bool disconnect(bool halting);
    bool condition();
    bool start(discharge_type type, bool waitForTest = false);
    bool stop(bool);
    double getVoltage();
    double getCurrent();
    uint16_t getRawVoltage();
    uint16_t getRawCurrent();
    conditioner_state getState();
    double getLastWattHours();
    uint8_t getConnectedBattID();
    battery_quick_test_status getQuickTestResult();
    double getHighRateTestResult();

private:
    uint8_t conditionerID;
    uint32_t cutoffVoltageCheckTimer;
    uint32_t powerReadTimer;
    uint32_t conditionTimer;
    uint32_t highRateTestStartTime;
    double highRateTestResult;
    bool waitForTest;
    uint32_t testWaitStartTime;
    battery_quick_test_status quickTestResult;
    //signed int condContinueTimer;
    conditioner_state state; //The state of the conditioner (what it's doing)
    load_config stage; //The stage of conditioning
    discharge_type action; //Whether we're doing a quick test or a full discharge

    Battery *connectedBattery;
    EventManager *eManager;
    double wattHours;

    void setStage(load_config);
    void setState(load_config);
    void setPins(load_config);
    void switchInverter(bool);
    bool inverterIsActive();
    void addWattHours(uint32_t, double, double);
};

#endif /* CONDITIONER_H_ */
